<!--
*  2021-03-15
*  纱线新增编号
-->
<template>
  <div id="app" class="weaving-no">
    <!-- 头部导航 -->
    <van-nav-bar
      :title="title"
      ref="header"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    >
    </van-nav-bar>
    <van-cell-group>
      <van-cell title="名称">
        <template>
          <van-field
            v-model="name"
            placeholder="输入名称"
            clearable
          ></van-field>
        </template>
      </van-cell>
      <van-cell title="编号">
        <template>
          <van-field v-model="no" placeholder="输入编号" clearable></van-field>
        </template>
      </van-cell>

      <van-cell
        title="纱织制式"
        :value="unitFormatVal"
        is-link
        @click="showPicker = true"
      ></van-cell>
      <van-cell title="回潮">
        <template>
          <van-field
            v-model="actMoistureRegain"
            placeholder="输入回潮"
            clearable
          ></van-field>
        </template>
      </van-cell>
    </van-cell-group>
    <van-cell-group class="weaving-no-btn">
      <van-button block type="info" class="bgColor" @click="onConfirmSubmit"
        >保 存</van-button
      >
    </van-cell-group>

    <van-popup v-model="showPicker" position="bottom">
      <van-picker
        show-toolbar
        value-key="label"
        :columns="columns"
        @confirm="onConfirm"
        @cancel="onCancel"
      />
    </van-popup>
  </div>
</template>

<script>
import { comeBack } from "@/utils/index";
import { getYarnStandardAdd } from "@/api/yarn";
// import dayjs from "dayjs";

export default {
  name: "yarnWeavingNo",
  data() {
    return {
      title: "新增纱线编号",
      no: "",
      unitFormat: "1",
      unitFormatVal: "公制",
      name: "",
      actMoistureRegain: "",
      showPicker: false,
      columns: [
        {
          label: "公制",
          value: 1,
        },
        {
          label: "英制",
          value: 2,
        },
        {
          label: "D制",
          value: 3,
        },
      ],
      showStatus: false,
    };
  },
  created() {
    const that = this;
    if (process.env.NODE_ENV === "production") {
      this.$nextTick(() => {
        that.initFn(that);
      });
    } else {
      window.apiready = function () {
        that.initFn(that);
      };
    }
  },
  methods: {
    //初始化
    initFn(that) {
      window.api.addEventListener({
        name:'viewdisappear'
      }, function() {
        window.api.closeWin();
      });
    },
    // 返回
    onClickLeft() {
      comeBack();
    },
    // 保存
    onConfirmSubmit() {
      const that = this;
      getYarnStandardAdd(
        {
          no: this.no,
          actMoistureRegain: this.actMoistureRegain,
          name: this.name,
          unitFormat: this.unitFormat,
        },
        () => {
          that.$toast({
            type: "success",
            message: "新增纱线成功",
            duration: 1500,
            onClose: () => {
              comeBack();
            },
          });
        }
      );
    },
    // 确认编号
    onConfirm(val) {
      this.unitFormat = val.value;
      this.unitFormatVal = val.label;
      this.showPicker = false;
    },
    onCancel() {
      this.showPicker = false;
    },
  },
};
</script>

<style lang="scss">
.weaving-no {
  display: flex;
  flex-direction: column;
  // padding-top: 22px;
  .van-cell {
    .van-cell__value {
      flex: 0 0 70%;
      .van-field {
        padding: 0;
        display: flex;
        .van-cell__value {
          flex: 1;
        }
        .van-field__control {
          text-align: right;
        }
      }
    }
  }
  .weaving-no-btn {
    padding: 16px;
    &.van-hairline--top-bottom::after,
    .van-hairline-unset--top-bottom::after {
      border-width: 0;
    }
  }
}
</style>
